package com.example.wishstar.repository;

import android.util.Log;

import com.example.wishstar.api.Api;
import com.example.wishstar.db.UserDao;
import com.example.wishstar.model.NewMemory;
import com.example.wishstar.utils.ApiResponse;
import com.google.gson.JsonObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddMemoryRepository {
    private String TAG = "AddMemoryRepository";
    private Api api;

    public AddMemoryRepository(Api api) {
        this.api = api;
    }

    public void addMemory(NewMemory newMemory) {
        JsonObject fieldMap = new JsonObject();
        fieldMap.addProperty("userId", newMemory.userId);
        fieldMap.addProperty("type", newMemory.type);
        fieldMap.addProperty("isRepeat", newMemory.isRepeat);
        fieldMap.addProperty("memoryTitle", newMemory.memoryTitle);
        fieldMap.addProperty("memoryDate", newMemory.memoryDate.toString());
        api.addMemory(RequestBody.create(MediaType.parse("application/json"), fieldMap.toString())).enqueue(new Callback<ApiResponse<Integer>>() {
            @Override
            public void onResponse(Call<ApiResponse<Integer>> call, Response<ApiResponse<Integer>> response) {
                Log.e(TAG, "addMemory->onResponse");
                if (response.body() != null) {
                    if (response.body().code == 200) {
                        Log.e(TAG, "AddMemory成功");
                    }
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Integer>> call, Throwable t) {
                Log.e(TAG, "addMemory->onFailure");
            }
        });

    }
}
